package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysRoleService;
import com.atguigu.common.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: SysRoleController
 * Package: com.atguigu.auth.controller
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/13 15:27
 * @Version 1.0
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
  
  @Autowired
  private SysRoleService sysRoleService;
  
  // 查询所有角色
  @ApiOperation("查询所有角色")
  @GetMapping("findAll")
  public Result<List<SysRole>> findAll() {
    List<SysRole> roleList = sysRoleService.list();
    return Result.ok(roleList);
  }
  
  // 条件分页查询
  @PreAuthorize("hasAuthority('bnt.sysRole.list')")
  @ApiOperation("条件分页查询")
  @GetMapping("{page}/{limit}")
  public Result pageQueryRole(@PathVariable Long page, @PathVariable Long limit,
                              SysRoleQueryVo sysRoleQueryVo) {
    // 调用service的方法实现
    // 1 创建Page对象，传递分页相关参数
    // page 当前页  limit 每页显示记录数
    Page<SysRole> pageParam = new Page<>(page, limit);
    
    // 2 封装条件，判断条件是否为空，不为空进行封装
    LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
    String roleName = sysRoleQueryVo.getRoleName();
    if (!StringUtils.isEmpty(roleName)) {
      // 封装 like模糊查询
      wrapper.like(SysRole::getRoleName, roleName);
    }
    // 3 调用方法实现
    // IPage<SysRole> pageModel = sysRoleService.page(pageParam, wrapper);
    return Result.ok(sysRoleService.page(pageParam, wrapper));
  }
  
  @PreAuthorize("hasAuthority('bnt.sysRole.add')")
  @ApiOperation(value = "新增角色")
  @PostMapping("save")
  public Result save(@RequestBody SysRole role) {
    boolean isSuccess = sysRoleService.save(role);
    return isSuccess ? Result.ok() : Result.fail();
  }
  
  @PreAuthorize("hasAuthority('bnt.sysRole.list')")
  @ApiOperation(value = "根据id查询")
  @GetMapping("get/{id}")
  public Result get(@PathVariable Long id) {
    SysRole role = sysRoleService.getById(id);
    return Result.ok(role);
  }
  
  @PreAuthorize("hasAuthority('bnt.sysRole.update')")
  @ApiOperation(value = "修改角色")
  @PutMapping("update")
  public Result updateById(@RequestBody SysRole role) {
    sysRoleService.updateById(role);
    return Result.ok();
  }
  
  @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
  @ApiOperation(value = "根据id删除角色")
  @DeleteMapping("remove/{id}")
  public Result remove(@PathVariable Long id) {
    sysRoleService.removeById(id);
    return Result.ok();
  }
  
  @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
  @ApiOperation(value = "根据id批量删除")
  @DeleteMapping("batchRemove")
  public Result batchRemove(@RequestBody List<Long> idList) {
    sysRoleService.removeByIds(idList);
    return Result.ok();
  }
}
